<template>
  <div class="constant-checkbox" >
    <a-checkbox-group v-model="checkData" :defaultValue="checkData">
      <a-checkbox :value="key" :key="key" v-for="(value, key) in data" >{{value}}</a-checkbox>
    </a-checkbox-group>
  </div>
</template>

<script>
  import {prefix} from '../config';
  import {constantNameMap} from '../../../constants';
  export default {
    name: prefix + 'ConstantCheckbox',
    props: {
      value: {
        type: String,
        default: ''
      },
      type: {
        type: String,
        require: true
      },
      invalidKeys: {
        type: String,
        default: ''
      }
    },
    watch: {
      value (val) {
        if (!val) {
          this.checkData = [];
          return;
        }
        let valStr = val;
        let typeDataStr = this.checkData.join(',');
        if (valStr === typeDataStr) return;
        this.$nextTick(() => {
          this.checkData = val.split(',');
        });
      },
      checkData () {
        this.updateValue();
      }
    },
    data () {
      return {
        checkData: this.value ? this.value.split(',') : []
      };
    },
    computed: {
      data () {
        let constant = constantNameMap[this.type];
        let retObj = {};
        if (constant) {
          let invalidKeys = ',' + this.invalidKeys + ',';
          for (let key in constant) {
            if (invalidKeys.indexOf(',' + key + ',') !== -1) {
              continue;
            }
            retObj[key] = constant[key];
          }
        }
        return retObj;
      }
    },
    methods: {
      updateValue () {
        this.$emit('input', this.checkData.join(','));
        this.$emit('change', this.checkData.join(','));
      }
    }
  };
</script>
